﻿//modify by guodengjie 20170724

using System;
using System.Data;
using System.Web.UI.WebControls;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;

namespace Web.Pages.YongShui._14.DAICHULGL
{
    public partial class DAICHUL : BasePage
    {
        #region 变量

        /// <summary>
        ///     访问数据库
        /// </summary>
        private readonly SpHelper _sphelper = new SpHelper();

        #endregion

        #region 属性

        public DataTable DataSourceTOP
        {
            get { return ViewState["DataSourceTOP"] as DataTable; }
            set { ViewState["DataSourceTOP"] = value; }
        }

        public DataTable DataSourceBOTTOM
        {
            get { return ViewState["DataSourceBOTTOM"] as DataTable; }
            set { ViewState["DataSourceBOTTOM"] = value; }
        }

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                InitControl();
            if (IsCallback)
            {
                gvDaiChuLi.DataSource = DataSourceTOP;
                gvDaiChuLi.DataBind();
                gvOther.DataSource = DataSourceBOTTOM;
                gvOther.DataBind();
            }
        }

        /// <summary>
        ///     下拉框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DrpLX_SelectIndexChange(object sender, EventArgs e)
        {
            BindGV();
        }

        protected void cmLaiHanLX2_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGV2();
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化
        /// </summary>
        private void InitControl()
        {
            var totalTable = _sphelper.P_YS_LH_DAICHULICX(BusinessTool.WType, BusinessTool.ZHANDIANBH(),
                Convert.ToInt32(DrpLX.Value), LoginInfo.CurrentUser.UserID, 2);
            if (totalTable != null && totalTable.Tables.Count > 0 && totalTable.Tables[0] != null &&
                totalTable.Tables[0].Rows.Count > 0)
            {
                var topcount = 0;
                var bottomcount = 0;
                var html = "";
                var htmlYW = "";
                foreach (DataRow dr in totalTable.Tables[0].Rows)
                    if (Convert.ToInt32(dr["i_laihanlx"]) > 6)
                    {
                        htmlYW += dr["s_laihanlxmc"] + "待处理共" + dr["zs"] + "条。&nbsp;&nbsp;";
                        bottomcount += Convert.ToInt32(dr["zs"]);
                    }
                    else
                    {
                        html += dr["s_laihanlxmc"] + "待处理共" + dr["zs"] + "条。&nbsp;&nbsp;";
                        topcount += Convert.ToInt32(dr["zs"]);
                    }
                TotalTd.InnerHtml = "&nbsp;&nbsp;" + html + "共有" + topcount + "条待处理。";
                TotalTd2.InnerHtml = "&nbsp;&nbsp;" + htmlYW + "共有" + bottomcount + "条待处理。";
                dclmsg.InnerText = "您还有待处理事项：";
            }
            else
            {
                dclmsg.InnerText = "您没有待处理事项：";
            }
            DataTable LaiHanLX1 = null;
            DataTable LaiHanLX2 = null;
            var ds = _sphelper.P_YS_LH_LAIHANLX(BusinessTool.WType);

            if (ds != null && ds.Tables.Count > 0)
            {
                LaiHanLX1 = ds.Tables[0].Clone();
                LaiHanLX2 = ds.Tables[0].Clone();
                int lx;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    var dr1 = LaiHanLX1.NewRow();
                    var dr2 = LaiHanLX2.NewRow();
                    lx = Convert.ToInt32(dr["I_LAIHANH"]);
                    //区分自来水,地下水,地表水
                    if (BusinessTool.WType == 0 && lx <= 20)
                    {
                        if (lx > 6)
                        {
                            dr2["I_LAIHANH"] = dr["I_LAIHANH"];
                            dr2["S_LAIHANLXMC"] = dr["S_LAIHANLXMC"];
                            LaiHanLX2.Rows.Add(dr2);
                        }
                        else
                        {
                            dr1["I_LAIHANH"] = dr["I_LAIHANH"];
                            dr1["S_LAIHANLXMC"] = dr["S_LAIHANLXMC"];
                            LaiHanLX1.Rows.Add(dr1);
                        }
                    }
                    else if (BusinessTool.WType == 1)
                    {
                        if (lx == 21 || lx == 22 || lx == 24 || lx == 27 || lx == 35 || lx == 36)
                        {
                            dr2["I_LAIHANH"] = dr["I_LAIHANH"];
                            dr2["S_LAIHANLXMC"] = dr["S_LAIHANLXMC"];
                            LaiHanLX2.Rows.Add(dr2);
                        }
                    }
                    else if (BusinessTool.WType == 2)
                    {
                        if (lx == 21 || lx == 22 || lx == 23 || lx == 24 || lx == 25 || lx == 26 || lx == 27 ||
                            lx == 28 || lx == 29 || lx == 30 || lx == 31 || lx == 32 || lx == 33 || lx == 34)
                        {
                            dr2["I_LAIHANH"] = dr["I_LAIHANH"];
                            dr2["S_LAIHANLXMC"] = dr["S_LAIHANLXMC"];
                            LaiHanLX2.Rows.Add(dr2);
                        }
                    }
                    if (BusinessTool.WType != 0)
                    {
                        gvDaiChuLi.Columns[6].Visible = false;
                        gvOther.Columns[6].Visible = false;
                        gvDaiChuLi.Columns[8].Width = Unit.Parse("36%");
                        gvOther.Columns[8].Width = Unit.Parse("36%");
                    }

                    //if (lx > 6)
                    //{
                    //    dr2["I_LAIHANH"] = dr["I_LAIHANH"];
                    //    dr2["S_LAIHANLXMC"] = dr["S_LAIHANLXMC"];
                    //    LaiHanLX2.Rows.Add(dr2);
                    //}
                    //else
                    //{
                    //    dr1["I_LAIHANH"] = dr["I_LAIHANH"];
                    //    dr1["S_LAIHANLXMC"] = dr["S_LAIHANLXMC"];
                    //    LaiHanLX1.Rows.Add(dr1);
                    //}
                }
            }
            DrpLX.DataSource = LaiHanLX1;
            DrpLX.TextField = "S_LAIHANLXMC";
            DrpLX.ValueField = "I_LAIHANH";
            DrpLX.DataBind();

            cmLaiHanLX2.DataSource = LaiHanLX2;
            cmLaiHanLX2.TextField = "S_LAIHANLXMC";
            cmLaiHanLX2.ValueField = "I_LAIHANH";
            cmLaiHanLX2.DataBind();

            DrpLX.Items.Insert(0, new ListEditItem("全部", "0"));
            cmLaiHanLX2.Items.Insert(0, new ListEditItem("全部", "0"));
            DrpLX.SelectedIndex = 0;
            cmLaiHanLX2.SelectedIndex = 0;

            BindGV();
            BindGV2();
        }

        /// <summary>
        ///     绑定ASPxGridView
        /// </summary>
        private void BindGV()
        {
            var zd = BusinessTool.WType != 0 || BusinessTool.ZHANDIANBH() == "00" ? null : BusinessTool.ZHANDIANBH();
            var ds = _sphelper.P_YS_LH_DAICHULICX(BusinessTool.WType, zd, Convert.ToInt32(DrpLX.Value),
                LoginInfo.CurrentUser.UserID, 0);


            if (ds != null && ds.Tables.Count > 0)
            {
                DataSourceTOP = ds.Tables[0];
                gvDaiChuLi.DataSource = DataSourceTOP;
                gvDaiChuLi.DataBind();
            }
            else
            {
                Response.End();
            }
        }

        private void BindGV2()
        {
            var zd = BusinessTool.WType != 0 || BusinessTool.ZHANDIANBH() == "00" ? null : BusinessTool.ZHANDIANBH();
            var ds1 = _sphelper.P_YS_LH_DAICHULICX(BusinessTool.WType, zd, Convert.ToInt32(cmLaiHanLX2.Value),
                LoginInfo.CurrentUser.UserID, 1);


            if (ds1 != null && ds1.Tables.Count > 0)
            {
                DataSourceBOTTOM = ds1.Tables[0];
                gvOther.DataSource = DataSourceBOTTOM;
                gvOther.DataBind();
            }
            else
            {
                Response.End();
            }
        }

        public string GetLCBHMC(int lcbh)
        {
            var mc = "";
            if (lcbh == (int) eLaiHanLCBH.受理)
                mc = "受理";
            if (lcbh == (int) eLaiHanLCBH.业务员)
                mc = "承办人";
            if (lcbh == (int) eLaiHanLCBH.主任)
                mc = BusinessTool.WType == 0 ? "主任" : "取水科长";
            if (lcbh == (int) eLaiHanLCBH.节水科)
                mc = "节水科";
            if (lcbh == (int) eLaiHanLCBH.计财科)
                mc = "计财科";
            if (lcbh == (int) eLaiHanLCBH.处长)
                mc = "处长";

            return mc;
        }

        #endregion

        protected void btnSX_Click(object sender, EventArgs e)
        {
            InitControl();
            BindGV();
            BindGV2();
        }
    }
}